/* Demo 2014-0106 */

var basePath = "http://" + location.host + "/dp/";
var httpsPath = "https://" + location.host + "/dp/";
var datamodule = {};
var i18nJson = {};
var configData = false;

(function($) {
	$.fn.extend({
		"createPagenation" : function(setting) {
			setting = $.extend({
				"recordSum" : "",
				"limit" : datamodule.searchParam.limit,
				"defaultPage" : datamodule.searchParam.pageIndex,
				"funName" : "goToPage",
				"needDetail" : true,
				"tagName" : [ "首页", "上一页", "下一页", "尾页" ],
				"callBack" : function(data) {
				}
			}, setting);
			var dom_this = $(this).get(0);
			var pageSum = Math.ceil(setting.recordSum / setting.limit);
			var pageContent = [];

			var curPage = setting.defaultPage;
			var startPage = (curPage - 2) > 0 ? curPage - 2 : 1;
			var endPage = (startPage + 4) < pageSum ? (startPage + 4)
					: pageSum;
			startPage = (endPage - 4) > 0 ? (endPage - 4) : 1;
			for ( var i = startPage; i <= endPage; i++) {
				pageContent.push(i);
			}
			renderPage(pageContent, curPage);

			function renderPage(pageContent, curPage) {

				if (parseInt(setting.recordSum) <= parseInt(setting.limit)) {
					return;
				}
				var fromTo = (curPage * setting.limit) > setting.recordSum ? setting.recordSum
						: (curPage * setting.limit);
				var resultDetail = ((curPage - 1) * setting.limit + 1)
						+ '-' + fromTo;
				if (pageSum != 1) {
					var pageStr = '<div class="page_box cls">';
					if (setting.needDetail) {
						pageStr = pageStr
								+ '<span class="page-count"><strong>'
								+ setting.recordSum
								+ '</strong> 个结果的第<strong>'
								+ resultDetail + '</strong> 个</span>';
					}
					pageStr += '<span class="page-num">';

					if (curPage == 1) {
						pageStr += '<a class="dis_prev_page"  href="javascript:void(0)"></a>';
					} else {
						pageStr += '<a class="first_page" href="javascript:javascript:'
								+ setting.funName
								+ '(1,'
								+ curPage
								+ ')">' + setting.tagName[0] + '</a>';
						pageStr += '<a  class="prev_page"href="javascript:'
								+ setting.funName
								+ '('
								+ (curPage - 1 > 0 ? curPage - 1 : 1)
								+ ','
								+ curPage
								+ ')">'
								+ setting.tagName[1] + '</a>';
					}
					for ( var i = 0; i < pageContent.length; i++) {
						pageStr += '<a page="'
								+ pageContent[i]
								+ '" class="'
								+ (pageContent[i] == curPage ? "current"
										: "") + '" href="javascript:'
								+ setting.funName + '('
								+ pageContent[i] + ',' + curPage
								+ ')">' + pageContent[i] + '</a>';
					}
					if (curPage == pageSum) {
						pageStr += '<a class="dis_next_page"  href="javascript:void(0)"></a>';
					} else {
						pageStr += '<a class="next_page" href="javascript:'
								+ setting.funName
								+ '('
								+ (curPage + 1 > pageSum ? pageSum
										: curPage + 1)
								+ ','
								+ curPage
								+ ')">' + setting.tagName[2] + '</a>';
						pageStr += '<a class="last_page" href="javascript:'
								+ setting.funName
								+ '('
								+ pageSum
								+ ','
								+ curPage
								+ ')">'
								+ setting.tagName[3]
								+ '</a>';
					}

					pageStr += '</span></div>';
					$(dom_this).html(pageStr);
				}
			}

			eval(setting.funName
					+ '= '
					+ '\r\n function(targetPage,curPage) {'
					+ '\r\n datamodule.searchParam.pageIndex=targetPage; '
					+ '\r\n datamodule.searchParam.startRecordNum=datamodule.getStartRecordNum(); '
					+ '\r\n datamodule.searchParam.endRecordNum=datamodule.getEndRecordNum(); '
					+ '\r\n setting.callBack(targetPage);};');
		}
	});
})($);

datamodule.searchParam = {
	pageIndex : 1,
	limit : 10
};

datamodule.getStartRecordNum = function() {
	return (datamodule.searchParam.pageIndex - 1)
			* datamodule.searchParam.limit + 1;
};

datamodule.getEndRecordNum = function() {
	return datamodule.searchParam.pageIndex * datamodule.searchParam.limit;
};

var initConfigParam = function() {
	dataTools.postAjax('Common.getconfig', {}, function(data) {
		if (nspStatus.SUCCESS == data.returnCode) {
			configData = data['configMap'];
		}
	}, {
		async : false
	});
	datamodule.searchParam.limit = configData.pageRecordNum
			|| datamodule.searchParam.limit;
	defaultRegex.phoneNumber = configData.phoneNumberRegex
			|| defaultRegex.phoneNumber;
	defaultRegex.mobileNumber = configData.mobileNumberRegex
			|| defaultRegex.mobileNumber;
	basePath = configData.basePath;
	httpsPath = configData.httpsPath;
};